// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import videoManage from "../../framework/platform/videoManage";
import mainManage from "../../framework/mainManage";
import sceneMediator from "../../framework/sceneMediator";
import GameConfig from "../../framework/GameConfig";
import Tool from "../../framework/Tool";
import AudioManage, {soundList} from "../../framework/AudioManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class compensationView extends cc.Component {

    @property(cc.Label)
    eachShowVideoTimes : cc.Label[] = [];

    @property(cc.Node)
    btnNode : cc.Node[] = [];

    @property(cc.Node)
    lockSkin : cc.Node[] = [];

    @property(cc.Node)
    templateNode : cc.Node = null;

    @property(cc.Node)
    eggList : cc.Node = null;

    @property(cc.Node)
    nodeList : cc.Node[] = [];

    @property(cc.Node)
    frameNode : cc.Node = null;

    @property(cc.Node)
    dragonBall : cc.Node = null;

    @property(cc.Node)
    clickNode : cc.Node = null;

    @property(cc.Node)
    picList : cc.Node[] = [];

    onLoad () {
        AudioManage.playSFX(soundList.colour);
        
        if(gameData.getRoleProcessSkin()[17] == 1 || gameData.colorfulPiece[2][4] == 1){
            this.clickNode.active = false;
            this.dragonBall.active = false;
        }

        cc.tween(this.node).to(0.1,{
            scale : 1
        }).call(()=>{

            for(let j = 0 ; j < 6 ; j++ ){
                for(let i = 0 ; i < 12 ; i ++){
                    let node = cc.instantiate(this.templateNode);
                    node.x = -1800 + 150 * (j % 2) + 300 * i;
                    node.y = cc.winSize.height / 2 - 150 * j;
                    node.parent = this.eggList;
                    node.active = true;
                }
            }

            for(let i in this.eachShowVideoTimes){

                if(parseInt(i) == 0 &&  gameData.seeVideoTimes[0] == 3){
                    this.btnNode[0].active = false;
                    this.lockSkin[0].active = true;
                    if(sceneMediator.getHighScene() != sceneMediator.getMaxLevel()){
                        sceneMediator.highLevel = sceneMediator.getMaxLevel();
                        sceneMediator.saveHighLevel();
                    }
                }else{
                    this.eachShowVideoTimes[0].string = gameData.seeVideoTimes[0] + ",3";
                }

                if(parseInt(i) == 1 &&  gameData.seeVideoTimes[1] == 3){
                    let arr = [];
                    for(let i = 0 ; i < 9 ;i++){
                        arr.push(gameData.getRoleProcessSkin()[i]);
                    }
                    this.btnNode[1].active = false;
                    this.lockSkin[1].active = true;
                    if(arr.indexOf(0) > -1){
                        for(let i = 0 ; i < 9 ;i++){
                            gameData.saveRoleSkin(i);
                        }
                    }
                }else{
                    this.eachShowVideoTimes[1].string = gameData.seeVideoTimes[1] + ",3";
                }

                if(parseInt(i) == 2 &&  gameData.seeVideoTimes[2] == 3){
                    let arr = [];

                    for(let i = 0 ; i < 14 ;i++){
                        arr.push(gameData.getPetProcessSkin()[i]);
                    }
                    this.btnNode[2].active = false;
                    this.lockSkin[2].active = true;
                    if(arr.indexOf(0) > -1){
                        for(let i = 0 ; i < 14 ;i++){
                            gameData.savePetSkin(i);
                        }
                    }
                }else{
                    this.eachShowVideoTimes[2].string = gameData.seeVideoTimes[2] + ",3";
                }
            }

            cc.tween(this.nodeList[0])
                .to(0.1,{y: cc.winSize.height / 2 - 150})
                .to(0.2,{y: cc.winSize.height / 2 - 110})
                .start();
            cc.tween(this.nodeList[1])
                .to(0.2,{x : -30})
                .to(0.2,{x : -50})
                .delay(0.2)
                .start();
            cc.tween(this.nodeList[2])
                .to(0.1,{x : 30})
                .to(0.05,{x : 50})
                .delay(0.05)
                .call(()=>{
                    cc.tween(this.nodeList[3])
                        .to(0.1,{x : 30})
                        .to(0.05,{x : 50})
                        .delay(0.05)
                        .call(()=>{
                            cc.tween(this.nodeList[4])
                                .to(0.1,{x : 30})
                                .to(0.05,{x : 50})
                                .delay(0.05)
                                .call(()=>{
                                    cc.tween(this.nodeList[5])
                                        .to(0.1,{x : 30})
                                        .to(0.05,{x : 50})
                                        .start()
                                }).start()
                        }).start()
                }).start()
        }).start();
    }


    order = 0;
    seeOver = false;    
    btnClickEvent(event,data){
       
        this.order = parseInt(data) - 1;
        gameData.seeVideoTimes[this.order] += 1;
        gameData.saveSeeVideoTimes();
        if(gameData.seeVideoTimes[this.order] == 3){
            this.btnNode[this.order].active = false;
            this.lockSkin[this.order].active = true;
            if(parseInt(data) == 1){
                setTimeout(()=>{
                    // let exp  = gameData.judgeAddExp(0,5);
                    // setTimeout(()=>{
                    //     gameData.addExpValue(exp);
                    // },1500);
                    sceneMediator.highLevel = sceneMediator.getMaxLevel();
                    sceneMediator.saveHighLevel();
                    mainManage.createToast("成功解锁所有关卡！");
                },1000);
            }
            if(parseInt(data) == 2){
                for(let i = 0 ; i < 9 ;i++){
                    gameData.saveRoleSkin(i,1);
                }
                mainManage.createToast("成功解锁所有皮肤！");
                let exp  = gameData.judgeAddExp(0,6);
                setTimeout(()=>{
                    gameData.addExpValue(exp);
                },1500);
            }
            if(parseInt(data) == 3){
                for(let i = 0 ; i < 14 ;i++){
                    gameData.savePetSkin(i,1);
                }
                mainManage.createToast("成功解锁所有宠物！");
                let exp  = gameData.judgeAddExp(0,7);
                setTimeout(()=>{
                    gameData.addExpValue(exp);
                },1500);
            }
        }else{
            this.eachShowVideoTimes[this.order].string = gameData.seeVideoTimes[this.order] + ",3";
        }

        this.seeOver = false;
    }

    callback = null;
    setCallBack(callback){
        this.callback = callback;
    }

    backHome(){
        this.node.destroy();
        if(this.callback != null){
            this.callback();
        }
    }

    exhibitionDragonBall(event){
        if(gameData.getRoleProcessSkin()[17] == 0 && gameData.colorfulPiece[2][4] == 0){
            let pos = event.target.position;
            cc.tween(event.target).to(0.3,{
                position : cc.v2(0,0)
            }).call(()=>{
                event.target.active = false;
                Tool.loadPrefab("prefabs/callDragonBall",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("callDragonBall").setCallBack(5,false,()=>{

                    },()=>{
                        event.target.x = pos.x;
                        event.target.y = pos.y;
                        event.target.active = true;
                    });
                    view.parent = cc.find("Canvas");
                })
            }).start();
        }
    }

    clickTVTimes = [0,0];
    clickTvBtn(event,data){
        if(parseInt(data) == 1 && this.clickTVTimes[0] < 3){
            this.clickTVTimes[0]++;
            if(this.clickTVTimes[0] == 3){
                this.picList[0].active = false;
                this.picList[1].active = true;
            }
        }

        if(parseInt(data) == 2 && this.clickTVTimes[1] < 3){
            this.clickTVTimes[1]++;
            if(this.clickTVTimes[1] == 3){
                this.picList[2].active = false;
                this.picList[3].active = true;
            }
        }

        if(this.clickTVTimes[0] == 3 && this.clickTVTimes[1] == 3){
            this.clickNode.active = false;
            cc.tween(this.frameNode).by(0.3,{
                angle : -10
            }).start();
        }
    }
}
